App({
  navigateToLogin: false,
  onLaunch: function() {
    const that = this;
    // 检测新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success(res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone10') != -1 || modelmes.search('iPhone11') != -1) {
          that.globalData.isPhoneX = true;
        };
      }
    });

    // wx.setTabBarBadge({//tabbar右上角添加文本
    //   index: 2, ////tabbar下标
    //   text: '2'//显示的内容
    // })
    /**
     * 初次加载判断网络情况
     * 无网络状态下根据实际情况进行调整
     */
    wx.getNetworkType({
      success(res) {
        const networkType = res.networkType
        if (networkType === 'none') {
          that.globalData.isConnected = false
          wx.showToast({
            title: '当前无网络',
            icon: 'loading',
            duration: 2000
          })
        }
      }
      
    });
    /**
     * 监听网络状态变化
     * 可根据业务需求进行调整  testdd
     */
    wx.onNetworkStatusChange(function(res) {
      if (!res.isConnected) {
        that.globalData.isConnected = false
        wx.showToast({
          title: '网络已断开',
          icon: 'loading',
          duration: 2000,
          complete: function() {
            that.goStartIndexPage()
          }
        })
      } else {
        that.globalData.isConnected = true
        wx.hideToast()
      }
    });
    
  },
  goLoginPageTimeOut: function() {
    if (this.navigateToLogin){
      return
    }
    wx.removeStorageSync('token')
    this.navigateToLogin = true
    setTimeout(function() {
      wx.navigateTo({
        url: "/pages/authorize/index"
      })
    }, 1000)
  },
  goStartIndexPage: function() {
    setTimeout(function() {
      wx.redirectTo({
        url: "/pages/index/index"
      })
    }, 1000)
  },  
  onShow (e) {
    this.globalData.launchOption = e
    // 保存邀请人
    if (e && e.query && e.query.inviter_id) {
      wx.setStorageSync('referrer', e.query.inviter_id)
      if (e.shareTicket) {
        // 通过分享链接进来
        wx.getShareInfo({
          shareTicket: e.shareTicket,
          success: res => {
            // console.error(res)
          }
        })
      }
    }
    this.navigateToLogin = false;
    this.getGoodsNo();
  },
  globalData: {                
    isConnected: true,
    launchOption: undefined,
    vipLevel: 0,
    goodsNo:0,
    shopNo:'S0001',
    isPhoneX:false,
  },
  setShopNo(no){
    let that = this;
    if(no && no != ''){
      // 如果是扫码进入，判断扫码与之前缓存商户id是否一致，一致，不删除缓存，不一致，删除购物车缓存
      wx.getStorage({
        key: 'shopNo',
        success: function (res) {
          if (res.data && res.data != '') {
            if (res.data != no ){
              wx.removeStorage({ key: 'shopCarInfo' });
              wx.removeStorage({ key: 'buyNowInfo' });
              that.globalData.goodsNo = 0;
            }
              
              wx.setStorageSync('shopNo', no);
          } else {
            wx.setStorageSync('shopNo', no);
          }
        },
        fail(res) {
          that.globalData.shopNo = no;
          wx.setStorageSync('shopNo', no);
        }
      })
    }else{
      wx.getStorage({
        key: 'shopNo',
        success: function (res) {
          if (res.data && res.data != '') {
            that.globalData.shopNo = res.data;
          } else {
            that.globalData.shopNo = 'S0001';
          }
        },
        fail(res) {
          that.globalData.shopNo = 'S0001';
        }
      })
    }
   
  },
  getGoodsNo() {
    let that = this
    wx.getStorage({
      key: 'shopCarInfo',
      success: function (res) {
        let list = res.data;
        if (list && list.length > 0){
          let no = 0;
          for (let listItem of list) {
            no += listItem.list.length;
          }
          that.globalData.goodsNo = no;
          wx.setTabBarBadge({
            index: 1,
            text: no +''
          })
        }
      },
      fail() {
        that.globalData.goodsNo = 0;
        wx.setTabBarBadge({
          index: 1,
          text: '0'
        })
      }
    })
  },
  // 复制文本
  copyText(text) {
    wx.setClipboardData({
      data: text,
      success: function (res) {
        wx.showToast({
          title: '已复制完成',
          duration: 1500,
        });
      }
    });
  },
  showLoadText(text,to){
    wx.showLoading({
      title: text,
    })

    setTimeout(function () {
      wx.hideLoading();
      if (to && to=='my'){
        wx.switchTab({
          url: "/pages/my/index"
        })
      }
    }, 1500)
  }
})
// {
      //   "pagePath": "pages/category/category",
      //   "iconPath": "images/nav/ic_catefory_normal.png",
      //   "selectedIconPath": "images/nav/ic_catefory_pressed.png",
      //   "text": "分类"
      // },